import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

export const useModelStore = defineStore('model', () => {
  const models = ref([])
  const loading = ref(false)
  const error = ref(null)
  
  async function fetchModels() {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.get('/api/models')
      models.value = response.data
      return models.value
    } catch (err) {
      console.error('获取模型列表失败:', err)
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }
  
  async function getModel(idOrName) {
    if (!idOrName) {
      console.warn('getModel: 参数为空')
      return null
    }
    
    // 先从缓存中查找
    const cachedModel = models.value.find(m => 
      m.id === idOrName || m.name === idOrName
    )
    if (cachedModel) {
      return cachedModel
    }
    
    // 从API获取
    try {
      const { data } = await axios.get(`/api/models/${idOrName}`)
      
      // 更新缓存
      const index = models.value.findIndex(m => 
        m.id === data.id || m.name === data.name
      )
      if (index >= 0) {
        models.value[index] = data
      } else {
        models.value.push(data)
      }
      
      return data
    } catch (error) {
      console.error(`获取模型失败:`, error)
      throw error
    }
  }
  
  async function createModel(modelData) {
    try {
      const response = await axios.post('/api/models', modelData)
      return response.data
    } catch (error) {
      console.error('创建模型失败:', error.response?.data || error.message)
      throw new Error(error.response?.data?.error || '创建模型失败')
    }
  }
  
  async function updateModel(id, modelData) {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.put(`/api/models/${id}`, modelData)
      const updatedModel = response.data
      
      // 更新缓存
      const index = models.value.findIndex(m => m.id === id)
      if (index >= 0) {
        models.value[index] = updatedModel
      }
      
      return updatedModel
    } catch (err) {
      console.error(`更新模型 "${id}" 失败:`, err)
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }
  
  async function deleteModel(id) {
    loading.value = true
    error.value = null
    
    try {
      await axios.delete(`/api/models/${id}`)
      
      // 更新缓存
      const index = models.value.findIndex(m => m.id === id)
      if (index >= 0) {
        models.value.splice(index, 1)
      }
      
      return true
    } catch (err) {
      console.error(`删除模型 "${id}" 失败:`, err)
      error.value = err.message
      throw err
    } finally {
      loading.value = false
    }
  }
  
  function getModelByName(name) {
    if (!name) {
      console.warn('getModelByName: 参数为空')
      return null
    }
    return models.value.find(model => model.name === name) || null
  }
  
  return {
    models,
    loading,
    error,
    fetchModels,
    getModel,
    createModel,
    updateModel,
    deleteModel,
    getModelByName
  }
}) 